﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AirQuality
{
    /// <summary>
    /// SettingWindows.xaml 的交互逻辑
    /// </summary>
    public partial class SettingWindows : Window
    {
        private static readonly string[] LocationName = { "Custom", "LeftTop", "LeftBottom", "RightTop", "RightBottom" };

        public SettingWindows()
        {
            InitializeComponent();

            string[] SerialPorts = SerialPort.GetPortNames();
            foreach (string item in SerialPorts)
            {
                int i = PortBox.Items.Add(item);
                if (item.CompareTo(Server.serialPort.PortName) == 0)
                {
                    PortBox.SelectedIndex = i;
                }
            }

            for (int i = 0; i < LocationName.Length; i++)
            {
                if (LocationName[i].CompareTo(Server.ConfigJson["location"].ToString()) == 0)
                {
                    LocalBox.SelectedIndex = i;
                }
            }
        }

        private void CloseWindow(object sender, RoutedEventArgs e)
        {
            if ((sender as Button).Name == "ok")
            {

                try
                {
                    Server.serialPort.PortName = PortBox.SelectedItem.ToString();
                }
                catch { }
                Server.ConfigJson["COM"] = PortBox.SelectedItem.ToString();
                Server.ConfigJson["location"] = LocationName[LocalBox.SelectedIndex];
                DialogResult = true;
            }
            else
            {
                DialogResult = false;
            }
            Close();
        }
    }
}
